package cn.duhengkui.national.valuelist.core.data.aop;



import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.duhengkui.national.valuelist.annotation.Command;
import cn.duhengkui.national.valuelist.annotation.Query;
import cn.duhengkui.national.valuelist.annotation.SQLParam;
import cn.duhengkui.national.valuelist.annotation.SQLParamList;
import cn.duhengkui.national.valuelist.annotation.SQLParamMap;
import cn.duhengkui.national.valuelist.core.data.util.GenericsUtils;



/**   
 * <table border="1">
 *	<tr><td>包名称：</td><td>cn.duhengkui.national.valuelist.core.data.aop</td></tr>
 *	<tr><td>类名称：</td><td>MethodUtil</td></tr>
 *	<tr><td>类描述：</td><td>dao方法工具和方法类型定义</td></tr>
 *	<tr><td>创建人：</td><td>杜恒奎</td></tr>
 *  <tr><td>创建时间：</td><td>2018年6月7日/下午3:15:08</td></tr> 
 *  <tr><td>版本：</td><td>1.0.0</td></tr>
 * </table>   
 *     
 */
public class MethodUtil {

	/**
	 * 默认方法类型
	 */
	public static int DEFAULT = 0;
	
	/**
	 * 查询方法类型
	 */
	public static int LIST = 1;
	
	/**
	 * 分页方法类型
	 */
	public static int PAGE = 2;
	
	/**
	 * 更新方法类型
	 */
	public static int UPDATE = 3;
	
	
	/**
	 * 删除方法类型
	 */
	public static int BATCH_DELETE = 4;
	
	/**
	 * 单一对象查询方法类型
	 */
	public static int SINGLE = 5;
	
	/**
	 * 返回map查询类型
	 */
	public static int FREE_LIST = 6;
	
	/**
	 * 批量执行数据
	 */
	public static int BATCH = 7;
	
	/**
	 * 返回map查询类型
	 */
	public static int FREE_PAGE = 8;

	/**
	 * 执行命令类型
	 */
	public static int COMMAND = 200;
	
	
	



	
	/**
	 * 获取方法类型
	 * @param method 方法
	 * @return
	 */
	public static int getExecuteType(Method method) {
		Query batch = method.getAnnotation(Query.class);
		Command command = method.getAnnotation(Command.class);
		if(batch != null) {
			if (batch.type().equals(Query.Type.LIST)) {
				return LIST;
			} else if (batch.type().equals(Query.Type.PAGE)) {
				return PAGE;
			} else if (batch.type().equals(Query.Type.MODIFY)) {
				return UPDATE;
			} else if (batch.type().equals(Query.Type.REMOVE)) {
				return BATCH_DELETE;
			} else if (batch.type().equals(Query.Type.SINGLE)) {
				return SINGLE;
			} else if (batch.type().equals(Query.Type.BANTCH)){
				return BATCH;
			} else if (batch.type().equals(Query.Type.FREE_LIST)){
				return FREE_LIST;
			} else {
				return DEFAULT;
			}
		} else if(command != null) {
			return COMMAND;
		} else {
			return DEFAULT;
		}
	}
	
	/**
	 * 获取方法所有参数Map
	 * @param arguments 参数数组
	 * @param method 方法
	 * @return
	 */
	public static Map<String, Object> getParameterMap(Object[] arguments, Method method) {

		Annotation[][] annotations = method.getParameterAnnotations();
		Map<String, Object> map = new HashMap<String, Object>();
		for(int i = 0 ; i < annotations.length ; i++) {
			if(annotations[i] != null && annotations[i].length > 0 && annotations[i][0] instanceof SQLParam) {
				map.put(((SQLParam) annotations[i][0]).value()
						, arguments[i]);
			} else if(annotations[i] != null && annotations[i].length > 0 && annotations[i][0] instanceof SQLParamMap && arguments[i] instanceof Map){
				@SuppressWarnings("unchecked")
				Map<String,Object> tmpMap = (Map<String,Object>)arguments[i];
				map.putAll(tmpMap);
			}
		}
		return map;
	}
	
	
	/**
	 * 获取批量方法所有参数List
	 * @param arguments 参数数组
	 * @param method 方法
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> getParameterList(Object[] arguments, Method method) {
		Annotation[][] annotations = method.getParameterAnnotations();
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		for(int i = 0 ; i < annotations.length ; i++) {
			if(annotations[i] != null && annotations[i].length > 0 && annotations[i][0] instanceof SQLParamList && arguments[i] instanceof List){
				resultList = (List<Map<String,Object>>)arguments[i];
				break;
			}
		}
		return resultList;
	}
	
	
	
	/**
	 * 获取方法返回值类型
	 * @param method 方法
	 * @return
	 */
	public static Class<?> getReturnType(Method method) {
		return GenericsUtils.getMethodGenericReturnType(method);
	}
	

	
}